package com.aios.util;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

import org.apache.log4j.Logger;

import com.aios.entity.unit.ConnectionEnum;

public class CheckConnection {

	private static final int SOCKET_TIME_OUT = 2000;
	
	private static final Logger logger = Logger.getLogger(CheckConnection.class);
	
	public static ConnectionEnum check(String url, Integer port) {
		
		SocketAddress sockaddr = new InetSocketAddress(url, port);
		Socket socket = new Socket();
		
		try {
			socket.connect(sockaddr, SOCKET_TIME_OUT);
		} catch (Exception e) {
			return ConnectionEnum.FAIL;
			
		} finally {
			try {
				socket.close();
			} catch (IOException e) {
				logger.error(e);
			}
		}
		
		return ConnectionEnum.OK;
	}

}
